package org.groupg.practice.swingx;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class DateRenderer extends DefaultTableCellRenderer {
    private final SimpleDateFormat dateFormat;

    public DateRenderer(String format) {
        super();
        dateFormat = new SimpleDateFormat(format);
    }

        @Override
        public Component getTableCellRendererComponent(
                JTable table, Object value,
                boolean isSelected, boolean hasFocus,
                int row, int column) {

            // 调用父类方法设置基础样式
            super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

            // 格式化日期值
            if (value instanceof Date) {
                setText(dateFormat.format((Date) value));
                setHorizontalAlignment(SwingConstants.CENTER);  // 设置居中对齐
            }
            return this;
        }
}
